package middleware

import (
	"net/http"
	roles_service "peilian-api/app/services/roles_servie"

	"github.com/gin-gonic/gin"
)

func VerificationUser() gin.HandlerFunc {
	return func(c *gin.Context) {
		roleId, exists := c.Get("rid")
		if !exists {
			c.JSON(http.StatusOK, gin.H{
				"code": 403,
				"data": struct{}{},
				"msg":  "No Auth",
			})
			c.Abort()
			return
		}
		uri := c.FullPath()
		isFlag := new(roles_service.RolesService).GetRolesByRUI(roleId.(uint), uri, c.Request.Method)
		if !isFlag {
			c.JSON(http.StatusOK, gin.H{
				"code": 403,
				"data": struct{}{},
				"msg":  "No Auth",
			})
			c.Abort()
			return
		}
		c.Next()
	}
}
